//package com.copm.ifm.common.mq.rabbit.task;
//
//import com.alibaba.fastjson.JSONObject;
//import com.copm.ifm.base.service.mq.constant.RabbitConstants;
//import com.copm.ifm.servers.sys.entity.BrokerMessageLog;
//import com.copm.ifm.servers.sys.entity.TOrder;
//import com.copm.ifm.servers.sys.mapper.BrokerMessageLogMapper;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.util.Date;
//import java.util.List;
//
///**
// * 定时排查，补偿消息
// * 如果重试次数count>=3   就进行再次发送
// * @author tp
// */
//@Component
//public class RetryMessageTasker {
//
//    @Autowired
//    private RabbitTemplate rabbitTemplate;
//
//    @Resource
//    private BrokerMessageLogMapper brokerMessageLogMapper;
//
//    @Scheduled(initialDelay = 5000, fixedDelay = 10000)
//    public void reSend(){
//        //pull status = 0 and timeout message
//        List<BrokerMessageLog> list = brokerMessageLogMapper.query4StatusAndTimeoutMessage();
//        list.forEach(messageLog -> {
//            if(messageLog.getTryCount() >= RabbitConstants.ORDER_SEND_MAX){
//                //update fail message
//                messageLog.setStatus(RabbitConstants.ORDER_SEND_FAILURE);
//                messageLog.setUpdateTime(new Date());
//                brokerMessageLogMapper.changeBrokerMessageLogStatus
//                        (messageLog.getMessageId(), RabbitConstants.ORDER_SEND_FAILURE, new Date());
//            } else {
//                // resend
//                messageLog.setTryCount(messageLog.getTryCount() + 1);
//                messageLog.setUpdateTime(new Date());
//                brokerMessageLogMapper.update4ReSend(messageLog.getMessageId(), new Date());
//                JSONObject jsonObject = JSONObject.parseObject(messageLog.getMessage());
//                TOrder reSendOrder = jsonObject.toJavaObject(TOrder.class);
//                try {
//                    rabbitTemplate.convertAndSend(reSendOrder);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    System.err.println("-----------异常处理-----------");
//                }
//            }
//        });
//    }
//}
